package net.sppan.blog.model;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.plugin.ehcache.IDataLoader;
import net.sppan.blog.commons.Constants;
import net.sppan.blog.model.base.BaseUser;
import net.sppan.blog.utils.ObjectUtils;
import net.sppan.blog.vo.UserVo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Generated by JFinal.
 */
@SuppressWarnings("serial")
public class User extends BaseUser<User> implements Serializable {
	public static final User dao = new User();

	public List<User> findUserByRoleId(Integer id) {
		List<User> users = new ArrayList<>();
		List<Record> records = Db.find("SELECT u.* FROM sys_user AS u,sys_user_role AS r WHERE u.id = r.user_id AND r.role_id = " + id);
		if (records != null && records.size() > 0) {
			for (Record record : records) {
				User u = new User();
				u.setId(record.getInt("id"));
				u.setCreatedate(record.getDate("createdate"));
				u.setDes(record.getStr("des"));
				u.setEmail(record.getStr("email"));
				u.setIcon(record.getStr("icon"));
				u.setName(record.getStr("name"));
				u.setParent(record.getInt("parent"));
				u.setPhone(record.getStr("phone"));
				u.setPwd(record.getStr("pwd"));
				u.setStatus(record.getInt("status"));
				u.setClickNumLimit(record.getInt("click_num_limit"));
				users.add(u);
			}
		}
		return users;
	}

	/**
	 * 从缓存中加载用户
	 * @param userId
	 * @return
	 */
	public User loadInSession(String userId) {
		if (StrKit.isBlank(userId)) {
			return null;
		}
		return loadInSession(Long.parseLong(userId));
	}

	/**
	 * 从缓存中加载用户
	 * @param userId
	 * @return
	 */
	public User loadInSession(final long userId) {
		return CacheKit.get(Constants.CacheName.session.get(), userId, new IDataLoader() {

			@Override
			public Object load() {
				return findById(userId);
			}
		});
	}

	/**
	 * 权限集
	 */
	public Set<String> getPermissionSets() {
		return Res.dao.getUserAllResKeySet(this.getId());
	}

	/**
	 * 用户登录
	 * @param userName
	 * @param pwd
	 * @return
	 */
	public User login(String userName, String pwd) {
		return this.findFirst("select * from sys_user where name = ? and pwd = ? and status = 1 limit 1", userName, pwd);
	}

	public List<User> findUserByOffer(Integer id) {
		List<OfferUser> ous = OfferUser.dao.find("select * from sys_offer_user where offer_id=" + id);
		List<String> strs = new ArrayList<>();
		if (ous != null && ous.size() > 0) {
			for (OfferUser ou : ous) {
				strs.add(ou.getUser());
			}
		}
		if (strs.contains("all")) {
			return null;
		}
		List<User> users = dao.find("select * from sys_user where id in(" + ObjectUtils.listToString(strs) + ")");
		return users;
	}

	public Page<UserVo> customPage(int pageNumber, int pageSize, String select, String sql) {
		Page<Record> records = Db.paginate(pageNumber, pageSize, select, sql);
		if (records != null && records.getList().size() > 0) {
			List<Record> rs = records.getList();
			List<UserVo> uvs = new ArrayList<>();
			for (Record record : rs) {
				UserVo vo = new UserVo();
				vo.setId(record.getInt("id"));
				vo.setCreatedate(record.getDate("createdate"));
				vo.setDes(record.getStr("des"));
				vo.setEmail(record.getStr("email"));
				vo.setHardware(record.getStr("hardware"));
				vo.setAppVersion(record.getStr("appVersion"));
				vo.setName(record.getStr("name"));
				vo.setParent(record.getInt("parent"));
				vo.setPhone(record.getStr("phone"));
				vo.setPwd(record.getStr("pwd"));
				vo.setRoleName(record.getStr("role_name"));
				vo.setStatus(record.getInt("status"));
				vo.setParentName(record.getStr("parent_name"));
				vo.setClickNumLimit(record.getInt("click_num_limit"));
				vo.setAccount(record.getStr("account"));
				uvs.add(vo);
			}
			Page<UserVo> page = new Page<>(uvs, records.getPageNumber(), records.getPageSize(), records.getTotalPage(), records.getTotalRow());
			return page;
		}
		return null;
	}

	public List<User> findUserByRoleId(int agentid, int administratorid) {
		List<User> users = new ArrayList<>();
		String str = agentid + "," + administratorid;
		List<Record> records = Db.find("SELECT u.* FROM sys_user AS u,sys_user_role AS r WHERE u.id = r.user_id AND r.role_id in (" + str + ")");
		if (records != null && records.size() > 0) {
			for (Record record : records) {
				User u = new User();
				u.setId(record.getInt("id"));
				u.setCreatedate(record.getDate("createdate"));
				u.setDes(record.getStr("des"));
				u.setEmail(record.getStr("email"));
				u.setIcon(record.getStr("icon"));
				u.setName(record.getStr("name"));
				u.setParent(record.getInt("parent"));
				u.setPhone(record.getStr("phone"));
				u.setPwd(record.getStr("pwd"));
				u.setStatus(record.getInt("status"));
				u.setClickNumLimit(record.getInt("click_num_limit"));
				users.add(u);
			}
		}
		return users;
	}

}
